layui.use(['element','table','form','layer','laydate'], function(){
    var table = layui.table,
    form = layui.form,
    $ = layui.jquery,
    laydate = layui.laydate,
    layer = layui.layer;

    loadCommunityList("");

    function loadCommunityList(communityId) {
        $.get('/cdysylives/secret/queryCommunityList', {}, function (result) {
            if(result != null){
                if(result.data != null){
                    var $opt_html = "<option value=''>请选择社区</option>";
                    $.each(result.data, function (index, item) {
                        $opt_html += "<option value='" + item.id + "'>" + item.name + "</option>";
                    });
                    $("#communityId").empty().append($opt_html);
                    if(communityId){
                        $("#communityId").val(communityId);
                    }
                    form.render('select');
                }
            }
        });
    }

    table.render({
        elem: '#secretRecordTable'
        ,url:'/cdysylives/secret/list'
        ,title: '秘钥列表'
        ,method: 'get'
        ,where: {}
        ,cols: [[
            {type: 'checkbox'},
            {field: 'xh',type:'numbers', title: '序号',}
            ,{field:'communityName', title:'社区名称',align:'center'}
            ,{field:'monitorPlatform', title:'开放平台名称',align:'center'}
            ,{field:'appKey', title:'应用Key',align:'center'}
            ,{field:'appSecret', title:'应用Secret',align:'center'}
            ,{field: 'isEnabled', title: '启用状态', align:'center',templet: function (d) {
                    return parseStatus(d.isEnabled);
                }}
            , {field: 'right', title: '操作', align:'center',width:200,templet:function(data){
                    return '<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>' +
                        '<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>';
                }
            }
        ]]
        ,page: true
    });

    $("#addSecret").on('click',function (){
        $("#secret_form")[0].reset();
        $("#communityId").attr("disabled", false);
        $("#saveSecret").attr("disabled", false);
        $("#saveSecret").attr("class", "layui-btn");
        $('#id').val("");
        layer.open({
            type: 1
            , title: '秘钥新增'
            , shadeClose: true
            , area: ['60%', '80%']
            , content: $('#addSecretDiv')
        });
    });
    //批量启用
    form.on('submit(enable)',function(obj){
        var checkStatus = table.checkStatus('secretRecordTable');
        var	data = checkStatus.data;
        if(data.length === 0){
            layer.tips("请至少选择一条记录!",this);
            return false;
        }
        var idArr = [];
        data.forEach(item=>{
            idArr.push(item.id);
        });
        $.ajax({
            url: "/cdysylives/secret/batchEnable",
            type: 'post',
            contentType: 'application/json',
            data: JSON.stringify(idArr),
            success: function (res) {
                if (res.success) {
                    layer.closeAll();
                    layer.msg(res.msg);
                    table.reload('secretRecordTable',{page: {curr:1}});
                } else {
                    layer.msg(res.msg)
                }
            }
        });
        return false;
    });

    //批量禁用
    form.on('submit(disable)',function(obj){
        var checkStatus = table.checkStatus('secretRecordTable');
        var	data = checkStatus.data;
        if(data.length === 0){
            layer.tips("请至少选择一条记录!",this);
            return false;
        }
        var idArr = [];
        data.forEach(item=>{
            idArr.push(item.id);
        });
        $.ajax({
            url: "/cdysylives/secret/batchDisable",
            type: 'post',
            contentType: 'application/json',
            data: JSON.stringify(idArr),
            success: function (res) {
                if (res.success) {
                    layer.closeAll();
                    layer.msg(res.msg);
                    table.reload('secretRecordTable',{page: {curr:1}});
                } else {
                    layer.msg(res.msg)
                }
            }
        });
        return false;
    });

    form.on('submit(saveSecret)',function(data){
        $("#saveSecret").attr("disabled", true);
        $("#saveSecret").attr("class", "layui-btn layui-btn-disabled");
        data.field.communityName = $("#communityId").find("option:selected").text();
        layer.load();
        $.ajax({
            type: 'post',
            url: '/cdysylives/secret/saveOrUpdate',
            contentType: "application/json;charset=utf-8",
            dataType: 'json',
            data: JSON.stringify(data.field),
            success: function (res) {
                layer.msg(res.msg);
                setTimeout(function (){
                    layer.closeAll();
                },1000);
                $("#saveSecret").attr("disabled", false);
                $("#saveSecret").attr("class", "layui-btn");
                querySecret();
            },
        });
        return false;
    });

    table.on('tool(secretRecordTable)', function(obj){
        let data = obj.data;
        switch (obj.event){
            case 'edit':
                $("#saveSecret").attr("disabled", false);
                $("#saveSecret").attr("class", "layui-btn");
                form.val("secret_form", data);
                $("#id").val(data.id);
                $("#communityId").attr("disabled", true);
                form.render();
                layer.open({
                    type: 1
                    , title: '编辑秘钥'
                    , shadeClose: true
                    , area: ['60%', '80%']
                    , content: $('#addSecretDiv')
                });
                break;
            case 'del':
                layer.confirm('确定要删除吗?', {
                }, function(){
                    layer.load();
                    $.get('/cdysylives/secret/delete',{id:data.id},function (res) {
                        layer.msg(res.msg,{
                            time:500,
                            end:function () {
                                querySecret();
                                layer.closeAll();
                            }
                        });
                    });
                });
                break;
        }
    });

    form.on('submit(querySecret)', function (data) {
        querySecret( data.field);
        return false;
    });

    function querySecret(fields){
        table.reload('secretRecordTable', {
            where: fields
            , page: {
                curr: 1
            }
        });
    }

    $("#cancelAdd").on('click',function (){
        layer.closeAll();
    });

});
function parseStatus(status) {
    switch (Number(status)) {
        case 1: return "启用";
        case 2: return "禁用";
        default: return "-";
    }
}

function reload(){
    location.reload();
}

